import cv2


class Camera:
    def __init__(self):
        self.cap = cv2.VideoCapture(0)  # 初始化摄像头

    def start_camera(self):
        if not self.cap.isOpened():
            print("错误：无法打开摄像头。")
            exit()

    def capture_frame(self):
        ret, frame = self.cap.read()  # 读取一帧视频
        if not ret:
            print("错误：无法读取帧。")
            exit()
        return frame

    def display_frame(self, frame, landmarks, gesture):
        # 使用OpenCV在帧上绘制手部关键点和手势信息
        if landmarks:
            for landmark in landmarks.landmark:
                x = int(landmark.x * frame.shape[1])
                y = int(landmark.y * frame.shape[0])
                cv2.circle(frame, (x, y), 5, (0, 255, 0), -1)
        cv2.putText(frame, gesture, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2, cv2.LINE_AA)
        cv2.imshow('手势识别', frame)

    def release(self):
        self.cap.release()  # 释放摄像头资源
        cv2.destroyAllWindows()  # 关闭所有OpenCV窗口
